import React from 'react'
import OrderFoodItem from '../orderFoodItem'
import { Button, Descriptions, message, Tag } from 'antd';
import { get } from '../../util/get'
import style from './index.module.css'
import { post } from '../../util/post';
/**
 * 显示一条订单，用于我的订单、店铺管理中的订单管理页面
 * @param {*} props 
 * @returns 
 */
export default function OrderVos(props) {
    const { type,data: { orderItems, order: { receiveAddress, receiveName, receivePhone, totalAmount, updateTime, status, orderSn } } } = props
    const statusArr = ['待付款', '待出货', '待收货', '已完成', '已取消', '已失效']
    const statusTag = ['warning','processing','processing','success','default','error']
    const token = localStorage.getItem('TOKEN')
    const handlePay = async () => {//处理用户支付
        const res = await get(`/order/alipayOrder?orderSn=${orderSn}`, token)
        const newWindow = window.open("","_self");
        newWindow.document.write(res.data);
        newWindow.focus();
    }
    const handleConfirm = async ()=>{//处理用户确认收货
        const res = await post(`/order/order/${orderSn}/receive`,{},token)
        const {code,msg}=res.data
        if(code===0)  {
            message.success(msg)
            const {r,setR} = props
            setR(!r);
        }
        else message.error(msg)
    }
    const handleDeliver = async()=>{//处理商家发货
      const res = await post(`/order/shop/${orderSn}/send`,{},token)
      console.log(res)
      const {code,msg}=res.data
      if(code===0)  {
        message.success(msg)
        const {r,setR} = props
        setR(!r);
    }
      else message.error(msg)
    }
    const handleArr = [handlePay,handleDeliver,handleConfirm];
    const renderButton = (status)=>{
        // console.log(type,status,'orderVos')
        if(status===1&&type===1)
        return <Button type='primary' className={style.submit} onClick={handleArr[status]}>出货</Button>
        else if(type===0&&status===0)  
        return <Button type='primary' className={style.submit} onClick={handleArr[status]}>去支付</Button>
        else if(type===0&&status===2)
        return <Button type='primary' className={style.submit} onClick={handleArr[status]}>确认收货</Button>
    }
    return (
        <div className={style.item}>
            {
                orderItems.map((i,index) => {
                    return <OrderFoodItem key={index} size={64} data={i}></OrderFoodItem>
                })
            }
        <div className={style.price}>
            <span>总金额</span>
            <span>¥{totalAmount}</span>
        </div>
            <Descriptions
                className={style.addressInfo}
                title={'收货信息'}>
                <Descriptions.Item label="收货人姓名">{receiveName}</Descriptions.Item>
                <Descriptions.Item label="联系号码">{receivePhone}</Descriptions.Item>
                <Descriptions.Item label="收货地址">{receiveAddress}</Descriptions.Item>
                <Descriptions.Item label="时间">{updateTime}</Descriptions.Item>
                <Descriptions.Item label="状态"><Tag color={statusTag[status]}>{statusArr[status]}</Tag></Descriptions.Item>
            </Descriptions>
            {
                renderButton(status)
            }
        </div>
    )
}
